package com.bootdo.program.shejiao.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.common.controller.BaseController;
import com.bootdo.program.shejiao.domain.MemberUserDO;
import com.bootdo.program.shejiao.service.MemberUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.program.shejiao.domain.WithdrawalDO;
import com.bootdo.program.shejiao.service.WithdrawalService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 用户提现表
 * 
 * @author guoyunfei
 * @email 2549789059@qq.com
 * @date 2021-10-18 15:08:39
 */
 
@Controller
@RequestMapping("/shejiao/withdrawal")
public class WithdrawalController extends BaseController {
	@Autowired
	private WithdrawalService withdrawalService;

	@Autowired
	private MemberUserService memberUserService;
	
	@GetMapping()
	@RequiresPermissions("shejiao:withdrawal:withdrawal")
	String Withdrawal(){
	    return "shejiao/withdrawal/withdrawal";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shejiao:withdrawal:withdrawal")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		query.put("appid",queryAppid());
		List<WithdrawalDO> withdrawalList = withdrawalService.list(query);
		int total = withdrawalService.count(query);
		PageUtils pageUtils = new PageUtils(withdrawalList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shejiao:withdrawal:add")
	String add(){
	    return "shejiao/withdrawal/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("shejiao:withdrawal:edit")
	String edit(@PathVariable("id") Long id,Model model){
		WithdrawalDO withdrawal = withdrawalService.get(id);
		model.addAttribute("withdrawal", withdrawal);
	    return "shejiao/withdrawal/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shejiao:withdrawal:add")
	public R save( WithdrawalDO withdrawal){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		if(withdrawalService.save(withdrawal)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shejiao:withdrawal:edit")
	public R update( WithdrawalDO withdrawal){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}

		withdrawalService.update(withdrawal);

		return R.ok();
	}

	/**
	 * 审核
	 */
	@ResponseBody
	@RequestMapping("/updateShenhe")
	@RequiresPermissions("shejiao:withdrawal:edit")
	public R updateShenhe( WithdrawalDO withdrawal){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		WithdrawalDO withdrawalDO = withdrawalService.get(withdrawal.getId());
		withdrawalService.update(withdrawal);

		MemberUserDO memberUserDO = memberUserService.getUserById(withdrawalDO.getUserId());
		if(withdrawal.getStatus() == 2){
			memberUserDO.setFrozenBalance(memberUserDO.getFrozenBalance().subtract(withdrawalDO.getMoney()));
		}else if(withdrawal.getStatus() == 3){
			memberUserDO.setFrozenBalance(memberUserDO.getFrozenBalance().subtract(withdrawalDO.getMoney()));
			memberUserDO.setBalance(memberUserDO.getBalance().add(withdrawalDO.getMoney()));
		}

		memberUserService.update(memberUserDO);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shejiao:withdrawal:remove")
	public R remove( Long id){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		if(withdrawalService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shejiao:withdrawal:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}
		withdrawalService.batchRemove(ids);
		return R.ok();
	}
	
}
